package com.example.onlinetaxi.ui.activity.Driver.MainPage;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.GetOrderCallBack;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;
import com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments.DriverCancelOrderFragment;
import com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments.DriverCompleteOrderFragment;
import com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments.DriverDoing1OrderFragment;
import com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments.DriverDoing2OrderFragment;
import com.example.onlinetaxi.ui.activity.MainPage.OrderFragments.CancelOrderFragment;
import com.example.onlinetaxi.ui.activity.MainPage.OrderFragments.CompleteOrderFragment;
import com.example.onlinetaxi.ui.activity.MainPage.OrderFragments.DoingOrderFragment;
import com.example.onlinetaxi.ui.activity.MainPage.OrderFragments.WaitingOrderFragment;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DriverOrderFragment extends Fragment implements View.OnClickListener {

    private View rootView;
    // fragments
    private DriverCancelOrderFragment driverCancelOrderFragment = new DriverCancelOrderFragment();
    private DriverDoing1OrderFragment driverDoing1OrderFragment = new DriverDoing1OrderFragment();
    private DriverDoing2OrderFragment driverDoing2OrderFragment = new DriverDoing2OrderFragment();
    private DriverCompleteOrderFragment driverCompleteOrderFragment = new DriverCompleteOrderFragment();
    // 订单导航栏控件中的布局、图片和按钮
    // 撤销订单
    private LinearLayout driverCancelOrderLayout;
    private ImageView driverCancelOrderImg;
    private TextView driverCancelOrderTv;
    // 等待接单的订单
    private LinearLayout driverDoing1OrderLayout;
    private ImageView driverDoing1OrderImg;
    private TextView driverDoing1OrderTv;
    // 正在执行的订单
    private LinearLayout driverDoing2OrderLayout;
    private ImageView driverDoing2OrderImg;
    private TextView driverDoing2OrderTv;
    // 完成的订单
    private LinearLayout driverCompleteOrderLayout;
    private ImageView driverCompleteOrderImg;
    private TextView driverCompleteOrderTv;
    // 请求参数
    private Map<String, String> map = new HashMap<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_driver_order, container, false);

        intiView();

        initEvent();

        // 关闭加载框
        DriverMainActivity.closeProgressDialog();

        return rootView;
    }

    private void initEvent() {
        // 初始化按钮
        driverCancelOrderLayout.setOnClickListener(this);
        driverDoing1OrderLayout.setOnClickListener(this);
        driverDoing2OrderLayout.setOnClickListener(this);
        driverCompleteOrderLayout.setOnClickListener(this);

        // 获取订单数据
        initOrderList();

        driverDoing1OrderFragment.setGetOrderCallBack(new GetOrderCallBack() {
            @Override
            public void refreshOrder() {
                // 重新获取订单信息
                initOrderList();
            }
        });
        driverDoing2OrderFragment.setGetOrderCallBack(new GetOrderCallBack() {
            @Override
            public void refreshOrder() {
                // 重新获取订单信息
                initOrderList();
            }
        });
        driverCancelOrderFragment.setGetOrderCallBack(new GetOrderCallBack() {
            @Override
            public void refreshOrder() {
                // 重新获取订单信息
                initOrderList();
            }
        });
        driverCompleteOrderFragment.setGetOrderCallBack(new GetOrderCallBack() {
            @Override
            public void refreshOrder() {
                // 重新获取订单信息
                initOrderList();
            }
        });
    }

    private void initOrderList() {
        // 从后端数据库获取所有订单信息
        map.put("phone", LocalStorage.phone);

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "order", "getOrderList");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            // 创建订单列表
            List<ReceivedOrderDto> doing1OrderList = new ArrayList<>();
            List<ReceivedOrderDto> doing2OrderList = new ArrayList<>();
            List<ReceivedOrderDto> cancelOrderList = new ArrayList<>();
            List<CompletedOrderDto> completeOrderList = new ArrayList<>();

            Map<String, String> responseMap = dataResponse.getData();
            Set<String> keys = responseMap.keySet();

            for (String key : keys) {
                // 将json字符串转换回orderDTO
                Gson gson = new Gson();
                CompletedOrderDto order = null;
                try {
                    // 将order的两个时间切换为指定格式
                    order = TimeUtil.setRightTimeFormat(gson.fromJson(responseMap.get(key), CompletedOrderDto.class));
                } catch (Exception e) {

                }

                if (order != null) {
                    // 将order分配到不同的列表
                    if (order.getState().equals("3") && !order.getCancelState().equals("2")) {
                        // 前往接车
                        doing1OrderList.add(order);
                    } else if (order.getState().equals("4")) {
                        // 送乘客前往终点
                        doing2OrderList.add(order);
                    } else if (order.getState().equals("3") && order.getCancelState().equals("2")) {
                        // 已取消的订单
                        cancelOrderList.add(order);
                    } else if (order.getState().equals("7") || order.getState().equals("8")) {
                        // 已完成订单
                        completeOrderList.add(order);
                    }
                }
            }
            // 将列表分发到每一个fragment
            driverDoing1OrderFragment.setOrderList(doing1OrderList);
            driverDoing2OrderFragment.setOrderList(doing2OrderList);
            driverCompleteOrderFragment.setOrderList(completeOrderList);
            driverCancelOrderFragment.setOrderList(cancelOrderList);
        }
    }

    private void intiView() {
        // 进行初始化
        driverCancelOrderLayout = rootView.findViewById(R.id.driver_cancel_order_layout);
        driverCancelOrderImg = rootView.findViewById(R.id.driver_cancel_order_img);
        driverCancelOrderTv = rootView.findViewById(R.id.driver_cancel_order_tv);

        driverDoing1OrderLayout = rootView.findViewById(R.id.driver_doing_1_order_layout);
        driverDoing1OrderImg = rootView.findViewById(R.id.driver_doing_1_order_img);
        driverDoing1OrderTv = rootView.findViewById(R.id.driver_doing_1_order_tv);

        driverDoing2OrderLayout = rootView.findViewById(R.id.driver_doing_2_order_layout);
        driverDoing2OrderImg = rootView.findViewById(R.id.driver_doing_2_order_img);
        driverDoing2OrderTv = rootView.findViewById(R.id.driver_doing_2_order_tv);

        driverCompleteOrderLayout = rootView.findViewById(R.id.driver_complete_order_layout);
        driverCompleteOrderImg = rootView.findViewById(R.id.driver_complete_order_img);
        driverCompleteOrderTv = rootView.findViewById(R.id.driver_complete_order_tv);

        // 设置刚开始的颜色
        driverDoing1OrderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
        driverDoing1OrderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
        driverCancelOrderTv.setTextColor(Color.GRAY);
        driverCancelOrderImg.setColorFilter(Color.GRAY);
        driverDoing2OrderTv.setTextColor(Color.GRAY);
        driverDoing2OrderImg.setColorFilter(Color.GRAY);
        driverCompleteOrderTv.setTextColor(Color.GRAY);
        driverCompleteOrderImg.setColorFilter(Color.GRAY);

        // 设置刚开始的fragment
        replaceFragment(driverDoing1OrderFragment);
    }

    // 进行碎片之间的切换
    private void replaceFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.driver_order_fragment, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.driver_cancel_order_layout:
                // 进行按钮样式的修改
                driverCancelOrderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                driverCancelOrderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                driverDoing1OrderTv.setTextColor(Color.GRAY);
                driverDoing1OrderImg.setColorFilter(Color.GRAY);
                driverDoing2OrderTv.setTextColor(Color.GRAY);
                driverDoing2OrderImg.setColorFilter(Color.GRAY);
                driverCompleteOrderTv.setTextColor(Color.GRAY);
                driverCompleteOrderImg.setColorFilter(Color.GRAY);
                replaceFragment(driverCancelOrderFragment);
                break;
            case R.id.driver_doing_1_order_layout:
                // 进行按钮样式的修改
                driverDoing1OrderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                driverDoing1OrderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                driverCancelOrderTv.setTextColor(Color.GRAY);
                driverCancelOrderImg.setColorFilter(Color.GRAY);
                driverDoing2OrderTv.setTextColor(Color.GRAY);
                driverDoing2OrderImg.setColorFilter(Color.GRAY);
                driverCompleteOrderTv.setTextColor(Color.GRAY);
                driverCompleteOrderImg.setColorFilter(Color.GRAY);
                replaceFragment(driverDoing1OrderFragment);
                break;
            case R.id.driver_doing_2_order_layout:
                // 进行按钮样式的修改
                driverDoing2OrderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                driverDoing2OrderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                driverDoing1OrderTv.setTextColor(Color.GRAY);
                driverDoing1OrderImg.setColorFilter(Color.GRAY);
                driverCancelOrderTv.setTextColor(Color.GRAY);
                driverCancelOrderImg.setColorFilter(Color.GRAY);
                driverCompleteOrderTv.setTextColor(Color.GRAY);
                driverCompleteOrderImg.setColorFilter(Color.GRAY);
                replaceFragment(driverDoing2OrderFragment);
                break;
            case R.id.driver_complete_order_layout:
                // 进行按钮样式的修改
                driverCompleteOrderTv.setTextColor(getResources().getColor(R.color.perfect_orange));
                driverCompleteOrderImg.setColorFilter(getResources().getColor(R.color.perfect_orange));
                driverDoing1OrderTv.setTextColor(Color.GRAY);
                driverDoing1OrderImg.setColorFilter(Color.GRAY);
                driverDoing2OrderTv.setTextColor(Color.GRAY);
                driverDoing2OrderImg.setColorFilter(Color.GRAY);
                driverCancelOrderTv.setTextColor(Color.GRAY);
                driverCancelOrderImg.setColorFilter(Color.GRAY);
                replaceFragment(driverCompleteOrderFragment);
                break;
        }
    }

    // 解决界面二次打开，内嵌fragment空白问题
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (driverDoing1OrderFragment != null) {
            FragmentManager f = getFragmentManager();
            if (f != null) {
                final FragmentTransaction ft = f.beginTransaction();
                if (ft != null) {
                    ft.remove(driverDoing1OrderFragment).commitAllowingStateLoss();
                }
            }
        }
    }
}